/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.spestimator.processes;

import com.compomics.respin.model.processes.respincommand.RespinCommandLine;
import com.compomics.respin.model.progress.RespinProcess;
import com.compomics.respin.model.progress.State;
import com.compomics.util.experiment.identification.SearchParameters;
import java.io.File;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Kenneth
 */
public class RespinRunner {

    private final File mgf;
    private final File parameters;
    private RespinCommandLine command;
    private final String projectName;
    private final File mergedFasta;
    private static final Logger LOGGER = Logger.getLogger(RespinRunner.class);
    private final File outputFolder;

    public RespinRunner(File outputFolder, File mergedFasta, File mgf, File parameters, String projectName) {
        this.mgf = mgf;
        this.parameters = parameters;
        this.projectName = projectName;
        this.mergedFasta = mergedFasta;
        this.outputFolder = outputFolder;
    }

    public void run() {
        try {
            SearchParameters searchParameters = SearchParameters.getIdentificationParameters(parameters);
            searchParameters.setFastaFile(mergedFasta);
            SearchParameters.saveIdentificationParameters(searchParameters, parameters);
            command = new RespinCommandLine(mgf, parameters, projectName);
            command
                    .setOutputDir(outputFolder);
            RespinProcess process = new RespinProcess(command);
            for (State state : State.values()) {
                if (!state.equals(State.CLOSED)) {
                    LOGGER.info(state.toString().toUpperCase());
                    state.prceed(process);
                }
            }
        } catch (Exception ex) {
            LOGGER.error(ex);
        }
    }

    public File getCpsOutputFile() {
        return new File(outputFolder.getAbsolutePath() + "/" + projectName + ".cps");
    }
}
